﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ChroniclesOfVivandia.UI.TileEngine
{
    class TilesetTexture
    {

        private Point _TileSize;
        private Texture2D _Texture;
        public Dictionary<int, TilesetTextureZone> Zones = new Dictionary<int,TilesetTextureZone>();

        /// <summary>
        /// Builds a tileset from a texture that is composed of tiles
        /// </summary>
        /// <param name="Texture">Texture to apply to this tileset</param>
        /// <param name="TileSize">Size of each tile in the tileset</param>
        public TilesetTexture(Texture2D Texture, Point TileSize)
        {
            this._Texture = Texture;
            this._TileSize = TileSize;
        }

        /// <summary>
        /// Returns the texture for this tileset
        /// </summary>
        public Texture2D Texture
        {
            get
            {
                return this._Texture;
            }
        }

        /// <summary>
        /// Returns the size of each tile in this tileset
        /// </summary>
        public Point TileSize
        {
            get
            {
                return this._TileSize;
            }
        }

        /// <summary>
        /// Returns the computed count of tiles in this tileset texture based on the tilesize
        /// </summary>
        public Point TilesCount
        {
            get
            {
                return new Point(this._Texture.Width / _TileSize.X, this._Texture.Height / _TileSize.Y);
            }
        }

        /// <summary>
        /// Returns the point in tileset which corresponds to the tileindex
        /// </summary>
        /// <param name="TileIndex">Index of the tile based on tileset texture from left to right and up to down</param>
        /// <returns></returns>
        public Point IndexToTile(Int16 TileIndex){
            return new Point((int)TileIndex % this.TilesCount.X, (int)Math.Floor((double)TileIndex / TilesCount.X));
        }

        /// <summary>
        /// Returns the rectangle that matches the tile pointed at
        /// </summary>
        /// <param name="Tile">Tile to draw</param>
        public Rectangle TileToRect(Point Tile)
        {
            return new Rectangle(Tile.X * _TileSize.X, Tile.Y * _TileSize.Y, _TileSize.X, _TileSize.Y);
        }



    }
}
